using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Images;
using MainView.MainControls.Chart;
using SExperiment;
using SProject;
using SProject.Paramter;
using SSettings;
using SWellPlate;

namespace MainView.Windows
{
	public partial class ReferenceCurveEditWindow : Window, IComponentConnector
	{
		private Hashtable hashTable = new Hashtable();

		public static readonly DependencyProperty RefModeProperty = DependencyProperty.Register("RefMode", typeof(ERefMode), typeof(ReferenceCurveEditWindow), new UIPropertyMetadata(ERefMode.Auto));

		protected Experiment Experiment;

		private Project CurrentProject;

		private HRMRefTarget CurrentHRMTarget;

		private List<SampleTargetItem> selectedTargets;

		public ERefMode RefMode
		{
			get
			{
				return (ERefMode)GetValue(RefModeProperty);
			}
			set
			{
				SetValue(RefModeProperty, value);
			}
		}

		public ReferenceCurveEditWindow(Experiment experiment)
		{
			Experiment = experiment;
			InitializeComponent();
			ucWellPlate.Wells = Experiment.Wells;
			UCChartAnalysisMeltingDiffCurve.SetBinding(BaseCurveChart.ColorModeProperty, new Binding("ColorMode")
			{
				Source = Experiment,
				Mode = BindingMode.TwoWay,
				NotifyOnSourceUpdated = true
			});
			UCChartAnalysisMeltingDiffCurve.SetBinding(BaseCurveChart.SubsetProperty, new Binding("CurrentSubset")
			{
				Source = Experiment,
				Mode = BindingMode.TwoWay,
				NotifyOnSourceUpdated = true
			});
			UCChartAnalysisMeltingDiffCurve.SetBinding(BaseCurveChart.HighLightProperty, new Binding("HighLight")
			{
				Source = Experiment.ExperimentSetting,
				Mode = BindingMode.TwoWay,
				NotifyOnSourceUpdated = true
			});
			UCChartAnalysisMeltingDiffCurve.SetBinding(BaseCurveChart.CurverWidthProperty, new Binding("CurverWidth")
			{
				Source = Experiment.ExperimentSetting,
				Mode = BindingMode.TwoWay,
				NotifyOnSourceUpdated = true
			});
			UCChartAnalysisMeltingDiffCurve.SetBinding(BaseCurveChart.ShowCurveToolTipsProperty, new Binding("ShowCurveToolTips")
			{
				Source = Experiment.ExperimentSetting,
				Mode = BindingMode.TwoWay,
				NotifyOnSourceUpdated = true
			});
			try
			{
				UCChartAnalysisMeltingDiffCurve.MinX = (from s in Experiment.Wells
					where s.Project != null
					select s.Project.BasicOption.Items.Select((BasicOptionItem s1) => ((HRMOptionItem)s1).StartTemperatureBefore).Min()).Min();
				UCChartAnalysisMeltingDiffCurve.MaxX = (from s in Experiment.Wells
					where s.Project != null
					select s.Project.BasicOption.Items.Select((BasicOptionItem s1) => ((HRMOptionItem)s1).EndTemperatureAfter).Max()).Max();
			}
			catch (Exception)
			{
				UCChartAnalysisMeltingDiffCurve.MinX = -1.0;
				UCChartAnalysisMeltingDiffCurve.MaxX = 1.0;
			}
			AddHandler(UCPlate.OnSelectionChangedRoutedEvent, new RoutedEventHandler(WellPlateSelectionChanged_RoutedEvent), true);
			initTabControl();
			ucWellPlate.MultiSelect = false;
		}

		private void initTabControl()
		{
			foreach (Project item2 in Experiment.CurrentSubset.GetAllProject())
			{
				string name = item2.BasicInfo.Name;
				List<HRMRefTarget> list = new List<HRMRefTarget>();
				foreach (HRMRefTarget item3 in Experiment.CurrentSubset.GetSubsetParamter(name).RefTarget)
				{
					HRMRefTarget item = item3.Copy();
					list.Add(item);
				}
				if (list.Count > 0)
				{
					hashTable.Add(name, list);
				}
				if (CurrentProject == null)
				{
					CurrentProject = item2;
				}
				List<string> list2 = item2.BasicOption.Items.Select((BasicOptionItem s) => s.TargetName).Distinct().ToList();
				TabItem tabItem = new TabItem();
				tabItem.Header = name;
				TabControl tabControl = new TabControl();
				foreach (string name2 in list2)
				{
					TabItem tabItem2 = new TabItem();
					if (CurrentHRMTarget == null)
					{
						CurrentHRMTarget = ((List<HRMRefTarget>)hashTable[name]).Where((HRMRefTarget s) => s.TargetName == name2).FirstOrDefault();
					}
					tabItem2.Header = name2;
					tabControl.Items.Add(tabItem2);
				}
				if (tabControl.Items.Count > 1)
				{
					tabItem.Content = tabControl;
				}
				myControl.Items.Add(tabItem);
			}
			myControl.SelectionChanged += myControl_SelectionChanged;
			if (myControl.Items.Count <= 1)
			{
				myControl.Visibility = Visibility.Collapsed;
				myGrid.Margin = new Thickness(8.0, 2.0, 8.0, 35.0);
			}
			ResetVisibleWells();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			foreach (DictionaryEntry item in hashTable)
			{
				Experiment.CurrentSubset.GetSubsetParamter((string)item.Key).RefTarget = item.Value as List<HRMRefTarget>;
			}
			base.DialogResult = true;
			Close();
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void WellPlateSelectionChanged_RoutedEvent(object sender, RoutedEventArgs e)
		{
			WellCell wellCell = ucWellPlate.WellCells.Where((WellCell s) => s.IsSelected).FirstOrDefault();
			if (wellCell == null)
			{
				return;
			}
			CurrentHRMTarget.ManualItem = wellCell.Well.Targets.Where((SampleTargetItem x) => x.TargetName == CurrentHRMTarget.TargetName).FirstOrDefault();
			DumpInvoke.DifferenceCurve(Experiment, selectedTargets, CurrentHRMTarget);
			foreach (SampleTargetItem item in selectedTargets)
			{
				ObservableCollection<ChartData> wellTargets = ucWellPlate.WellTargets;
				Func<ChartData, bool> predicate = (ChartData s) => s.SampleTargetItem == item;
				ChartData chartData = wellTargets.Where(predicate).FirstOrDefault();
				if (chartData != null)
				{
					chartData.IsVisible = true;
					chartData.WellCell.IsInfoSelected = true;
				}
			}
			double num = double.NaN;
			double num2 = double.NaN;
			foreach (SampleTargetItem selectedTarget in selectedTargets)
			{
				num = Utility.GetMin(num, UCChartAnalysisMeltingDiffCurve.GetCurve(selectedTarget).GetCurveMinY());
				num2 = Utility.GetMax(num2, UCChartAnalysisMeltingDiffCurve.GetCurve(selectedTarget).GetCurveMaxY());
			}
			if (!double.IsNaN(num) && !double.IsNaN(num2))
			{
				double num3 = (ConfigSettings.GetInstance().CurveSetting.MaxYTimes - 1.0) * (num2 - num);
				UCChartAnalysisMeltingDiffCurve.MinY = num - num3;
				UCChartAnalysisMeltingDiffCurve.MaxY = num2 + num3;
			}
			num = double.NaN;
			num2 = double.NaN;
			List<SampleTargetItem> list = (from s in ucWellPlate.WellTargets
				where s.IsVisible && s.WellCell.IsInfoSelected
				select s.SampleTargetItem).ToList();
			foreach (SampleTargetItem item2 in list)
			{
				num = Utility.GetMin(num, UCChartAnalysisMeltingDiffCurve.GetCurve(item2).GetCurveMinX());
				num2 = Utility.GetMax(num2, UCChartAnalysisMeltingDiffCurve.GetCurve(item2).GetCurveMaxX());
			}
			if (!double.IsNaN(num) && !double.IsNaN(num2))
			{
				UCChartAnalysisMeltingDiffCurve.MinX = num;
				UCChartAnalysisMeltingDiffCurve.MaxX = num2;
			}
			UCChartAnalysisMeltingDiffCurve.RefreshSeries(true);
		}

		private void StackPanel_Checked(object sender, RoutedEventArgs e)
		{
			if (ucWellPlate != null && radio_Auto != null)
			{
				ResetSellectWell();
			}
		}

		private void ResetSellectWell()
		{
			Well well = null;
			well = ((RefMode != 0) ? ucWellPlate.Wells.Where((Well s) => s.Targets.Contains(CurrentHRMTarget.ManualItem)).FirstOrDefault() : ucWellPlate.Wells.Where((Well s) => s.Targets.Contains(CurrentHRMTarget.AutoItem)).FirstOrDefault());
			ucWellPlate.SelectWell(well);
			CurrentHRMTarget.RefMode = RefMode;
		}

		private void myControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			TabItem selectProject = myControl.SelectedItem as TabItem;
			TabControl tabControl = (TabControl)selectProject.Content;
			TabItem selectTarget = ((tabControl == null) ? null : (tabControl.SelectedItem as TabItem));
			if (selectProject == null)
			{
				return;
			}
			if (!selectProject.Header.ToString().Equals(CurrentProject.BasicInfo.Name))
			{
				CurrentProject = (from s in Experiment.CurrentSubset.GetAllProject()
					where s.BasicInfo.Name.Equals(selectProject.Header.ToString())
					select s).SingleOrDefault();
				if (tabControl == null)
				{
					string TargetName = CurrentProject.BasicOption.Items.Select((BasicOptionItem s) => s.TargetName).Distinct().ToList()
						.FirstOrDefault();
					CurrentHRMTarget = ((List<HRMRefTarget>)hashTable[CurrentProject.BasicInfo.Name]).Where((HRMRefTarget s) => s.TargetName.Equals(TargetName)).FirstOrDefault();
					ResetVisibleWells();
				}
			}
			else if (tabControl != null && !selectTarget.Header.ToString().Equals(CurrentHRMTarget.TargetName))
			{
				CurrentHRMTarget = ((List<HRMRefTarget>)hashTable[CurrentProject.BasicInfo.Name]).Where((HRMRefTarget s) => s.TargetName.Equals(selectTarget.Header.ToString())).FirstOrDefault();
				ResetVisibleWells();
			}
		}

		private void ResetVisibleWells()
		{
			Subset subset = new Subset();
			selectedTargets = GetTargets(CurrentProject, CurrentHRMTarget.TargetName);
			foreach (SampleTargetItem item in selectedTargets)
			{
				List<Well> wells = Experiment.CurrentSubset.Wells;
				Func<Well, bool> predicate = (Well s) => s.Targets.Contains(item);
				Well well = wells.Where(predicate).SingleOrDefault();
				if (well != null)
				{
					subset.Wells.Add(well);
				}
			}
			ucWellPlate.Subset = subset;
			UCChartAnalysisMeltingDiffCurve.Targets = ucWellPlate.WellTargets.Where((ChartData s) => selectedTargets.Contains(s.SampleTargetItem)).ToList();
			RefMode = CurrentHRMTarget.RefMode;
			ResetSellectWell();
		}

		public List<SampleTargetItem> GetTargets(Project prj, string targetName)
		{
			List<Well> list = Experiment.CurrentSubset.Wells.Where((Well s) => s.Project == prj).ToList();
			List<SampleTargetItem> list2 = new List<SampleTargetItem>();
			foreach (Well item in list)
			{
				foreach (SampleTargetItem target in item.Targets)
				{
					if (!(target.TargetName != targetName) && !item.IsInValid && target.SampleType != SampleType.Negative && target.SampleType != SampleType.NTC)
					{
						list2.Add(target);
					}
				}
			}
			return list2;
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		internal Delegate _CreateDelegate(Type delegateType, string handler)
		{
			return Delegate.CreateDelegate(delegateType, this, handler);
		}
	}
}
